<?
class news{
   static function add($new_title,$new_image,$new_summary='',$new_description='',$new_hot=0,$new_status=1){
      if($new_title != '' ){
         $sql= " INSERT INTO news(
                        new_title,
                        new_images,
                        new_date,
                        new_summary,
                        new_description,
                        new_hot,
                        new_status
                        )
               VALUES('" . $new_title . "','" . $new_image . "'," . time() . ",'" . $new_summary . "','" . $new_description . "'," . $new_hot . "," . $new_status . ")";
         $db_exe = new db_execute($sql);
         unset($db_exe);                        
      }
   }
   static function view($new_id){
      $result = array();
      $db_sel = new db_query("SELECT * 
                              FROM news 
                              WHERE new_id=" . $new_id);
      $result = $db_sel->resultArray();
      unset($db_sel);
      return $result;
   }
   static function delete($new_id){
      $db_exe = new db_execute("DELETE 
                                FROM news 
                                WHERE new_id=" . $new_id);
      unset($db_exe);
   }
   static function update($new_id,$array=array()){
      if(isset($new_id) && count($array) > 0){
            $set_sql = self::sql_update($new_id,$array);
            echo $set_sql;
            //$db_exe = new db_execute($set_sql);
            //unset($db_exe);
      }
   }
   static function search($keyword){
      $result = array();
      if($keyword != ''){
         if(str_word_count($keyword) >= 4){
            $query = "SELECT *,MATCH(new_title) AGAINST('" . $keyword . "' IN BOOLEAN MODE) as point   
                      FROM news
                      WHERE MATCH(new_title) AGAINST('" . $keyword . "' IN BOOLEAN MODE)
                      HAVING point >= 1
                      ORDER BY point DESC";
         }else{
            $query = "SELECT * 
                      FROM news WHERE new_title LIKE'%" . $keyword . "%'";
         }
         $db_sel = new db_query($query);
         $result = $db_sel->resultArray();
         unset($db_sel); 
      }
      return $result;
   }
   protected function sql_update($id,$array=array()){
      $query = "UPDATE news";
      $set_query = " ";
      foreach($array as $key=>$value){
         if($key == 'new_date' || $key == 'new_status' || $key == 'new_hot')
            $set_query .= $key . "=" . $value . ",";
         else
            $set_query .= $key . "='" . $value . "',";    
      }
      $query .= ' SET ' . $set_query;
      return substr($query,0,-1) . " WHERE new_id=" . $id;
   }
 }
?>
